import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import Login from './components/pages/user/Login';
import AdminLayout from './components/mainlayout/AdminLayout';
import UserLayout from './components/mainlayout/UserLayout';
import AssetList from './components/pages/asset/AssetList';
import BorrowList from './components/pages/borrow/BorrowList';
import BorrowListUsr from './components/pages/borrow/BorrowListUsr';
import BorrowForm from './components/pages/borrow/BorrowForm';
import BorrowApprovalList from './components/pages/borrow/BorrowApprovalList';
import ReturnList from './components/pages/return/ReturnList';
import ReturnApprovalList from './components/pages/return/ReturnApprovalList';
import DepartmentList from './components/pages/department/DepartmentList';
import UserList from './components/pages/user/UserList';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import FileManage from './components/pages/file/FileManage';
import ReturnBorrowUsr from './components/pages/return/ReturnBorrowUsr';
import DepartmentUsageCalendar from './components/pages/statistics/DepartmentUsageCalendar';

// 管理员路由守卫，确保只有管理员可以访问
const AdminRoute = ({ children }) => {
  const user = JSON.parse(sessionStorage.getItem('user') || '{}');
  if (!user.uId) return <Navigate to="/login" replace />;
  return user.admin === 'yes' ? children : <Navigate to="/user" replace />;
};

// 用户路由守卫
const UserRoute = ({ children }) => {
  const user = JSON.parse(sessionStorage.getItem('user') || '{}');
  if (!user.uId) return <Navigate to="/login" replace />;
  return user.admin === 'yes' ? <Navigate to="/admin" replace /> : children;
};

const App = () => {
  return (
    <ConfigProvider locale={zhCN}>
      <Router>
        <Routes>
          {/* 未登录时走Login路由 */}
          <Route path="/login" element={<Login />} />        
          {/* 根路由重定向 */}
          <Route path="/" element={<Navigate to="/login" />} />

          {/* 登录之后才会走下面的，管理员路由 */}
          <Route path="/admin" element={<AdminRoute><AdminLayout /></AdminRoute>}>
            <Route index element={<Navigate to="/admin/assets" />} />
            <Route path="assets" element={<AssetList />} />
            <Route path="users" element={<UserList />} />
            <Route path="departments" element={<DepartmentList />} />
            <Route path="borrows" element={<BorrowList />} />
            {/* <Route path="returns" element={<ReturnList />} /> */}
            <Route path="borrow-approval" element={<BorrowApprovalList />} />
            <Route path="return-approval" element={<ReturnApprovalList />} />
            <Route path="filemanage" element={<FileManage />} />
            <Route path="borrow" element={<BorrowForm />} />
            <Route path="visualization" element={<DepartmentUsageCalendar />} />
          </Route>

          {/* 登录之后才会走下面的，用户路由 */}
          <Route path="/user" element={<UserRoute><UserLayout /></UserRoute>}>
            <Route index element={<Navigate to="/user/assets" />} />
            <Route path="assets" element={<AssetList />} />
            <Route path="borrows" element={<BorrowListUsr />} />
            <Route path="returns" element={<ReturnBorrowUsr />} />
            <Route path="borrow" element={<BorrowForm />} />
            <Route path="filemanage" element={<FileManage />} />
          </Route>
        </Routes>
      </Router>
    </ConfigProvider>
  );
};

export default App; 